package com.example.hzq.zqb.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.hzq.zqb.R;
import com.example.hzq.zqb.bean.CommentBean;

import java.util.List;

/**
 * Created by hzq on 2017/7/10.
 */

public class CommentAdapter extends RecyclerView.Adapter {
    private Context context;
    private LayoutInflater layoutInflater;//动态加载布局
    private List<CommentBean> list;//保存要显示的数据
    public CommentAdapter(Context context) {
        this.context=context;
        layoutInflater=LayoutInflater.from(context);
    }
    public void setList(List<CommentBean> list){
        this.list=list;
        notifyDataSetChanged();
    }
    public class ViewHolder extends RecyclerView.ViewHolder{
        private TextView username,comment,time;
        public ViewHolder(View itemView) {
            super(itemView);
            username=(TextView)itemView.findViewById(R.id.cm_username);
            comment=(TextView)itemView.findViewById(R.id.usercomment);
            time=(TextView)itemView.findViewById(R.id.cm_commenttime);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = layoutInflater.inflate(R.layout.commentitem, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
         CommentBean commentBean = list.get(position);
        if (commentBean == null)
            return;
         ViewHolder viewholder = (ViewHolder) holder;
        viewholder.username.setText(commentBean.getU_id());
        viewholder.comment.setText(commentBean.getContent());
        viewholder.time.setText(commentBean.getCreatetime());
    }

    @Override
    public int getItemCount() {
        if(list==null)
            return 0;
        return list.size();
    }
}
